package com.xudm.cdg.config;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class MybatisPlusConfig implements MetaObjectHandler {
    public static final String CREATED_BY = "createdBy";
    public static final String CREATED_TIME = "createdTime";
    public static final String UPDATED_BY = "updatedBy";
    public static final String UPDATED_TIME = "updatedTime";
    public static final String DELETE_FLAG = "deleteFlag";
    public static final String REVISION = "f_revision";

    public static final String INSERT_FILL_ERROR_LOG = "自动填充值发生异常。{}";
    public static final String SYSTEM = "system";

    /**
     * 新增分页拦截器，并设置数据库类型为mysql
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        Map<String, Object> fieldsToFill = new HashMap<>();
        fieldsToFill.put(CREATED_BY, SYSTEM);
        fieldsToFill.put(CREATED_TIME, LocalDateTimeUtil.now());
        fieldsToFill.put(UPDATED_BY, SYSTEM);
        fieldsToFill.put(UPDATED_TIME, LocalDateTimeUtil.now());
        fieldsToFill.put(DELETE_FLAG, 0);

        // 遍历填充字段
        for (Map.Entry<String, Object> entry : fieldsToFill.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            try {
                this.setFieldValByName(key, value, metaObject);
                // this.strictInsertFill(metaObject, CREATED_BY, Long.class, 1L);
            } catch (Exception e) {
                log.error(INSERT_FILL_ERROR_LOG, e.getMessage());
            }
        }
        log.info("insert fill end.");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        Map<String, Object> fieldsToFill = new HashMap<>();
        fieldsToFill.put(UPDATED_BY, SYSTEM);
        fieldsToFill.put(UPDATED_TIME, LocalDateTimeUtil.now());

        // 遍历填充字段
        for (Map.Entry<String, Object> entry : fieldsToFill.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            try {
                this.setFieldValByName(key, value, metaObject);
                // this.strictUpdateFill(metaObject, UPDATED_BY, Long.class, 1L);
            } catch (Exception e) {
                log.error(INSERT_FILL_ERROR_LOG, e.getMessage());
            }
        }
        log.info("update fill end.");
    }
}
